import axios, { AxiosInstance } from 'axios'
import { handleError } from './handleError'

function createRequestInstance(getServerUrl: () => string): AxiosInstance {
	const instance = axios.create({
		baseURL: `${getServerUrl()}/`,
		timeout: 10000,
		withCredentials: true,
	})
	instance.interceptors.response.use(
		async (res) => {
			return res
		},
		async (err) => {
			err = await handleError(err)
			return Promise.reject(err)
		},
	)
	return instance
}

export default createRequestInstance
